package server;

import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;

    /*private Map<String,HttpServlet> servletMap;

    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }*/

    private Map<String, List<String>> hostMap = new HashMap<>();
    private Map<String, Object> classMap = new HashMap<>();
    private Map<String, String> contextMap = new HashMap<>();
    private Map<String, String> wrapperMap = new HashMap<>();

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());


            List<String> contextList = hostMap.get(request.getHots());

            if (contextList == null || contextList.size() ==0) {
                response.output(HttpProtocolUtil.getHttpHeader404());
            }

            boolean contextState = false;
            for (String context : contextList) {
                if (!context.equals(request.getContext())) continue;
                contextState = true;
                break;
            }

            if (!contextState) {
                response.output(HttpProtocolUtil.getHttpHeader404());
            }

            String wrapper = contextMap.get(request.getContext());
            if (wrapper == null || "".equals(wrapper)) {
                response.output(HttpProtocolUtil.getHttpHeader404());
            }

            String className = wrapperMap.get(wrapper);
            Object object = classMap.get(className);
            HttpServlet myServlet = (HttpServlet) object;
            if (myServlet == null) {
                response.output(HttpProtocolUtil.getHttpHeader404());
            }
            myServlet.service(request, response);

            // 静态资源处理
//            if(servletMap.get(request.getUrl()) == null) {
//                response.outputHtml(request.getUrl());
//            }else{
//                // 动态资源servlet请求
//                HttpServlet httpServlet = servletMap.get(request.getUrl());
//                httpServlet.service(request,response);
//            }

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public Map<String, List<String>> getHostMap() {
        return hostMap;
    }

    public void setHostMap(Map<String, List<String>> hostMap) {
        this.hostMap = hostMap;
    }

    public Map<String, Object> getClassMap() {
        return classMap;
    }

    public void setClassMap(Map<String, Object> classMap) {
        this.classMap = classMap;
    }

    public Map<String, String> getContextMap() {
        return contextMap;
    }

    public void setContextMap(Map<String, String> contextMap) {
        this.contextMap = contextMap;
    }

    public Map<String, String> getWrapperMap() {
        return wrapperMap;
    }

    public void setWrapperMap(Map<String, String> wrapperMap) {
        this.wrapperMap = wrapperMap;
    }
}
